import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Request } from 'express';
import { join } from 'path';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class IndexHtmlGuard implements CanActivate {
    private indexHtmlPath: string;
    constructor(
        private readonly config: ConfigService,
    ) {
        this.indexHtmlPath = join(__dirname, '../../', 'static', 'index.html');
        console.log('indexPath', this.indexHtmlPath); 
    }
    canActivate(context: ExecutionContext): boolean {
        const request: Request = context.switchToHttp().getRequest();
        console.log(request.url);
        const isApiRequest = request.url.startsWith(this.config.get('app.prefix')+'/');
        if (!isApiRequest) {
            const response = context.switchToHttp().getResponse();
            response.sendFile(this.indexHtmlPath);
        }
        return true;
    }
}